﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.Views.Controls
{
    public partial class MessageBarControl : System.Web.UI.UserControl
    {
        private string _message;

        private bool _show;

        protected void Page_Init(object sender, EventArgs e)
        {
            Visible = false;
            EnableViewState = false;
        }

        public void Show(Exception exception)
        {
            while (exception != null)
            {
                if (!(exception is System.Reflection.TargetInvocationException))
                    _message = _message + exception.Message;
                exception = exception.InnerException;
            }
            _show = true;
            Visible = true;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "MessageBar");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            string imageUrl = "~/App_Themes/" + Page.Theme;
            if (_show)
                imageUrl = imageUrl + "/ErrorIcon.gif";
            else
                imageUrl = imageUrl + "/InformationIcon.gif";
            writer.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveClientUrl(imageUrl));
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            writer.Write(" ");
            writer.WriteEncodedText(_message);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
        }
    }
}